import toonModule from '../../../assets/js/toonModule.js'
import util from '../../../assets/js/util'
import Toast from 'toon-ui/lib/components/toast'
export default {
  data() {
    return {
        detailData: {
          "content":" 门城的清晨阳光明媚，好久没有心情这么好了。", //内容
          "photos":[],  // 图片
          "createDate":"", // 创建时间
          "praised": true,  // 是否被赞过
          "praiseCount": 0,  // 赞数
          "commentCount": 0 // 评论数
        },
        commentList: [], // 评论列表
        curPage: 0,
        pageNum: '1',  //当前页
        pageSize: '10', // 每页个数
        isShowContent: false // 显示详情状态,
    }
  },
  filters: {
      "changeTimeYMD": function(value){
        return util.dateFormat('yyyy-MM-dd', value)
      },
      "changeTimeHM": function(value){
        return util.dateFormat('hh:mm', value)
      }
  },
  mounted() {
    let _this = this;
    this.init();
    this.$util.setTitle('大赞门城');
   
  },
  methods: {
    init() {
      // 初始化 
        this.curPage = 0;
        this.pageNum = 1; //当前页
        this.pageSize = 10; // 每页个数
        this.content = ''; // 清除评论
        this.ShowContent = false; 
        this.commentList = [];
      // 获取详情
      this.getDetailById();
      
    },
    /**
     * 获取详情
     */
    getDetailById() {
      let _this = this;
      let id = _this.$route.params.id;
      let params = {
        "id": id
      }
      this.$http.post('/praisingCity/getDetailById', params).then(function (rep) {
        console.log(rep);
        if(rep.meta && rep.meta.code == '0') {
            // 详情数据的替换
            _this.detailData = rep.data;
            _this.detailData.createDate = _this.$util.dateFormat('yyyy-MM-dd hh:mm',rep.data.createDate)
            // 显示详情
            _this.isShowContent = true;
            // 获取评论列表
            _this.getCommentListById();
             setTimeout(function() {
                _this.$util.droploadUpOnScroll(_this.getCommentListById, 'more', 'mainScroll')
              }, 0);
        }
      }).catch(function (err) {

      })
    },
    /**
     * 获取评论列表
     */
    getCommentListById() {
      let _this = this;
      let id = _this.$route.params.id;

      if(_this.curPage == _this.pageNum) {
        return 
      }else {
        _this.curPage = _this.pageNum
      }

      let params = {
        "id": id,
        "pageNum": _this.pageNum,
        "pageSize": _this.pageSize
      }
      _this.$http.post('/praisingCity/getCommentListById', params).then(function (rep) {
        if(rep.meta && rep.meta.code == '0') {
            if(rep.data.length > 0) {
              _this.commentList = _this.commentList.concat(rep.data);
            }
            if(rep.data.length == _this.pageSize ) {
              _this.pageNum++;
            }
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    /**
     * 
     */
    praise() {
      let _this = this; 
      let id = _this.$route.params.id;
      let params = {
        'id': id
      }
      this.$http.post('/praisingCity/praise', params).then(function(rep){
            _this.detailData.praised = !_this.detailData.praised;
            console.log( _this.detailData.praised)
            if(_this.detailData.praised) {
               _this.detailData.praiseCount = _this.detailData.praiseCount - 0 + 1;
            }else {
               _this.detailData.praiseCount = _this.detailData.praiseCount - 0 - 1;
            }
               console.log(JSON.stringify(_this.detailData, null, 2))
           
        
      }).catch(function(err){
          console.log(err)
      })
    },
    /**
     * 提交评论
     */
    addComment() {
      let _this = this;
      let id = _this.$route.params.id;
      let content = _this.content;
      // 内容为空
      if (content == '' || !content) {
        Toast({
          'message': '评论内容不能为空',
          'duration': 2000
        })
      } else {
        _this.choose(function (userInfo) {
          // 如果点击取消
          if (userInfo.data.result == 'cancel') {

          } else {
            let params = {
              "commentAvator": userInfo.data.avatarId,
              "commentName": userInfo.data.title,
              "content": content,
              "feedId": userInfo.data.feedId,
              "id": id
            }
            _this.$http.post('/praisingCity/addComment', params).then(function(rep){
                if(rep.meta && rep.meta.code == '0') {
                  _this.init();
                }
            }).catch(function(err){
                alert(JSON.stringify(err));
            })  
            
          }

        });
      }
    },
    open() {
      toonModule.openCamera(function (data) {
        alert(JSON.stringify(data));
      })
    },
    choose(callback) {
      let _this = this;
      toonModule.chooseCard(function (data) {
        callback(data);
      })
    }

  },
  components: {

  }
}
